﻿using System;
using System.Linq;
using System.Threading;
using MdcService.Static;
using MdcService.Structure;
using MdcService.Structure.Enum;

namespace MdcService.Machine
{
	public class TestingMachine : BaseMachine
	{
		private bool _isconnected = false;

	    public override bool IsConnected => _isconnected;
	    public override MachineType Type { get; set; } = MachineType.Testing;

		public override void Connect()
		{
			if (_isconnected)
			{
				throw new Exception("Already connected");
			}
			if (true)
			{
				Thread.Sleep(200);
				_isconnected = true;
				LastConnected = DateTime.Now;
			}
			else
			{
				throw new Exception("Connection Error");
			}
		}

		public override void Disconnect()
		{
			if (!_isconnected)
			{
				throw new Exception("Already disconnected");
			}
			_isconnected = false;
		}

		public override void Read()
		{
			if (!_isconnected)
			{
				throw new Exception("Not connected");
			}
			Random r = new Random();
			Thread.Sleep(40);
			Values.ForEach(i =>
			{
			    i.Value.Value = (r.NextDouble()*100).ToString();
			});
			LastRead = DateTime.Now;
		}

		public override string ToString()
		{
			return $"Testing Server {Name} with {Values.Count} values";
		}
	}
}
